<?php

class Db_Tables2_Accounts
{
	var $server_id_start = 1; //开始
	var $server_id_end = 270; //最后一个
	
	function __construct() {
		$this->maozhua_wb = Zend_Registry::get("maozhua_wb");
	}
	
	function GetAccounts($user_id)
	{
		$result = array();
		
		$select = $this->maozhua_wb->select();
		$select->from("accounts", "*");
		$select->where("user_id = ?", $user_id);
		$rows = $this->maozhua_wb->fetchAll($select);
		
		if($rows)
		{
			foreach($rows as $row)
			{
				$result[$row['account_type']] = array(
					"account_id" => $row['account_id'],
					"user_name" => $row['user_name'],
					"pwd" => $row['pwd'],
					"status" => $row['status'],
					"err_login_times" => $row['err_login_times'],
					"dealer_id" => $row['dealer_id']
				);
			}
		}
		
		return $result;
	}
	
	function AvailableServerId($account_type)
	{
		$result = FALSE;
		
		$select = $this->maozhua_wb->select();
		$select->from("accounts", array("server_id"));
		$select->where("account_type = ?", $account_type);
		$rows = $this->maozhua_wb->fetchAll($select);
		
		$used_server_ids = array();
		
		if(!empty($rows))
		{
			foreach($rows as $row)
			{
				$used_server_ids[] = $row['server_id'];
			}
		}
		
		$server_id_pool = array();
		
		for($n=$this->server_id_start;$n<=$this->server_id_end;$n++)
		{
			$server_id_pool[] = $n;
		}
		
		$diff = array_diff($server_id_pool, $used_server_ids);
		
		if(!empty($diff))
		{
			$rk = array_rand($diff);
			
			if(is_numeric($rk))
			{
				$result = $diff[$rk];
			}
		}
		
		return $result;
	}
}